package com.ecommerce.dal.common;

import java.util.Date;
import java.util.List;

import com.ecommerce.dal.entity.Client;
import com.ecommerce.dal.entity.ClientHistory;
import com.ecommerce.dal.entity.ClientHistoryId;
import com.ecommerce.dal.entity.Company;
import com.ecommerce.dal.entity.Rank;
import com.ecommerce.dal.service.impl.ClientManagerService;
import com.ecommerce.dal.service.impl.CompanyManagerService;
import com.ecommerce.dal.service.impl.RankManagerService;

public class TestInheritedClient {

	public TestInheritedClient(ClientManagerService clientManagerService,
			RankManagerService rankManagerService,
			CompanyManagerService companyManagerService) {

		/**
		 * general Note: don't forget to switch fetching strategy type in
		 * ClientHistory to Eager fetching, in order for this code to run
		 * properly
		 */
		Client client = new Client();
		client.setFirstName("ClientTestInherit");

		/** Must Increment here ... **/
		long id = 16;
		client.setUserName("belal" + id);
		client.setPassword("123");
		client.setLastName("galal");
		client.setNewsletter(true);
		client.setBirthDate(new Date());
		client.setGender("male");
		client.setJoinDate(new Date());
		// client.setSponsor(false);

		Rank rank = rankManagerService.retrieveRank(1);
		Company company = companyManagerService.retrieveCompany(1L);

		client.setRank(rank);
		client.setCompany(company);

		clientManagerService.saveClient(client);
		System.out.println("Done inserting client");

		/** select , make sure PK exists **/
		// Client client2 = clientManagerService.retrieveClient(5L);
		Client client2 = clientManagerService.retrieveClient("belal" + id);
		System.out.println("retrieval of client2 succeeded");
		// Company company2 = clientManagerService.retrieveClientCompany(5L);
		Company company2 = clientManagerService.retrieveClientCompany("belal"
				+ id);
		System.out.println("retrieval of client2 based on company succeeded ");
		// System.out.println(client2);
		// System.out.println(company2.getCompanyName());
		// int i = clientManagerService.retrieveClientState(5L);
		Rank rank2 = clientManagerService.retrieveClientRank(id);
		System.out.println("retrieval of rank based on client succeeded");
		int i = clientManagerService.retrieveClientState("belal" + id);
		System.out.println("Client Name: " + client2.getFirstName()
				+ " , \nClient Company : " + company2.getCompanyName()
				+ " , \nClient Rank: " + rank2.getRankName()
				+ " , \nClient State: " + i);
		System.out.println("Client in Customer Table? "
				+ clientManagerService.checkIfCustomer(client2.getUserId()));
		System.out.println("Client already exists? "
				+ clientManagerService.checkIfExists(client2.getUserId()));
		List<Client> sponsorClients = clientManagerService
				.retrieveSponsorClients();
		for (Client c : sponsorClients) {
			System.out.println("Sponsor Clients: " + c.getFirstName());
		}
		System.out.println("Client whos card 9375041707 is: "
				+ clientManagerService
						.retrieveMembershipCardClient(9375041707L)
						.getUserName());
		/** update **/
		client2.setFirstName("ahmed");
		clientManagerService.updateClient(client2);

		
		
		
		
		
		/** Client History */
		ClientHistory clientHistory = new ClientHistory();
		clientHistory.setClient(client2);
		
		/** Build Client History Id! */
		ClientHistoryId clientHistoryId = new ClientHistoryId();
		clientHistoryId.setClientId(client2.getUser().getUserId());
		/** -------------------------- */
		
		clientHistory.setCurrentPaymentDate(new Date());
		clientHistory.setNextPaymentDate(new Date());
		clientHistory.setPaymentAmount(100.0);
		clientHistory.setId(clientHistoryId);
		clientManagerService.saveClientHistory(clientHistory);
		/** ---------------- */
		/** retrieve clientHistory based on Id */
		List<ClientHistory> clientHistoryList = clientManagerService
				.retrieveClientHistory(id);
		for (ClientHistory clientHistoryX : clientHistoryList) {
			System.out.println("retrieve clientHistory based on Id:\n\n"
					+ "Client Payment Ammount: "
					+ clientHistoryX.getPaymentAmount() + " \nClient Id: "
					+ clientHistoryX.getClient().getUserId());
		}
		List<ClientHistory> clientHistoryList2 = clientManagerService
				.retrieveClientHistory("belal" + id);
		for (ClientHistory clientHistoryX : clientHistoryList2) {
			System.out.println("retrieve clientHistory based on Id:\n\n"
					+ "Client Payment Ammount: "
					+ clientHistoryX.getPaymentAmount() + " \nClient Name: "
					+ clientHistoryX.getClient().getFirstName());
		}
		/** ---------------- */

	}

}
